package com.android.infantschool.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.nfc.Tag;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.infantschool.R;
import com.android.infantschool.bean.DynamicBean;
import com.android.infantschool.ui.imageloader.GlideRoundTransform;
import com.android.infantschool.util.ImageUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Administrator
 * @particulars
 * @time 2019\6\19 0019 8:59
 * @class describe
 */
public class DynamicImgAdapter extends RecyclerView.Adapter<DynamicImgAdapter.MyHolder> {

    private Context mContext;
    private List<DynamicBean.DataBean.UrlArrayBean> list = null;
    private OnImgLister lister;

    public DynamicImgAdapter(Context context, OnImgLister lister) {
        this.mContext = context;
        this.list = new ArrayList<>();
        this.lister = lister;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_dynamic_img, viewGroup, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @SuppressLint("CheckResult")
    @Override
    public void onBindViewHolder(@NonNull final MyHolder viewHolder, final int position) {
        DynamicBean.DataBean.UrlArrayBean bean = list.get(position);

        if (bean.getType() == 1) {
            viewHolder.imgPlay.setVisibility(View.GONE);
            Glide.with(mContext)
                    .load(bean.getPath())
                    .placeholder(R.mipmap.icon_glide_loading)
                    .error(R.mipmap.icon_glide_filed)
                    .into(viewHolder.imgView);
        }

        if (bean.getType() == 2) {
            viewHolder.imgPlay.setVisibility(View.VISIBLE);
            Glide.with(mContext)
                    .load(bean.getThumbnail())
                    .thumbnail(0.5f)
                    .placeholder(R.mipmap.icon_glide_loading)
                    .error(R.mipmap.icon_glide_filed)
                    .into(viewHolder.imgView);
        }

        viewHolder.imgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(getClass().getSimpleName(), "当前选择的图片:" + position);
                if (lister != null) {
                    lister.imgLister(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    /**
     * 刷新数据
     *
     * @param list
     */
    public void setData(List<DynamicBean.DataBean.UrlArrayBean> list) {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param list
     */
    public void addData(List<DynamicBean.DataBean.UrlArrayBean> list) {
        this.list.addAll(0, list);
        this.notifyDataSetChanged();
    }

    /**
     * 清除数据
     */
    public void clear() {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.notifyDataSetChanged();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        public ImageView imgView;
        public ImageView imgPlay;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            imgView = itemView.findViewById(R.id.img_dynamic_ad);
            imgPlay = itemView.findViewById(R.id.img_play);
        }
    }

    public interface OnImgLister {
        void imgLister(int position);
    }
}